package qs.openxt.libs.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.List;
import java.util.UUID;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Base64;

import qs.openxt.libs.App;

public class PictureUtil {
	/**
	 * 把bitmap转换成String
	 *
	 * @param filePath
	 * @return
	 */
	public static String bitmapToString(String filePath) {
		Bitmap bm = getSmallBitmap(filePath);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);
		byte[] b = baos.toByteArray();
		return Base64.encodeToString(b, Base64.DEFAULT);
	}

	/**
	 * 计算图片的缩放值
	 *
	 * @param options
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
		int w = reqWidth, h = reqHeight;
		final int height = options.outHeight;
		final int width = options.outWidth;
		if (height < width) {
			reqWidth = h;
			reqHeight = w;
		}
		int inSampleSize = 1;
		if (height > reqHeight || width > reqWidth) {
			final int heightRatio = Math.round((float) height / (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSampleSize;
	}

	/**
	 * 根据路径获得突破并压缩返回bitmap用于显示
	 *
	 * @param filePath
	 * @return
	 */
	public static Bitmap getSmallBitmap(String filePath) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, options);
		options.inSampleSize = calculateInSampleSize(options, 480, 800);
		options.inJustDecodeBounds = false;

		return BitmapFactory.decodeFile(filePath, options);
	}

	/**
	 * 获取保存图片的目录
	 *
	 * @return
	 */
	public static File getAlbumDir(String uuid) {
		String foldername = Utils.getExternalStorageDirectory() + "/" + App.PATH_NAME + "/images/" + uuid;
		File folder = new File(foldername);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return folder;
	}

	/**
	 * 获取启动图片保存图片的目录
	 *
	 * @return
	 */
	public static File getSplashScreen() {
		String foldername = Utils.getExternalStorageDirectory() + "/" + App.PATH_NAME + "/splashscreen";
		File folder = new File(foldername);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return folder;
	}

	public static String getFilePath(String filePath) throws Exception {
		if (filePath.indexOf("file:///") != -1)
			filePath = filePath.replace("file://", "");
		FileOutputStream fos = null;
		try {
			String suffix = filePath.substring(filePath.lastIndexOf(".") + 1);
			if ("jpg".equalsIgnoreCase(suffix)
					|| "jpeg".equalsIgnoreCase(suffix)
					|| "png".equalsIgnoreCase(suffix)
					|| "bmp".equalsIgnoreCase(suffix)) {
				String fileDir = UUID.randomUUID().toString().substring(24);
				Bitmap bm = PictureUtil.getSmallBitmap(filePath);
				File _f = new File(PictureUtil.getAlbumDir(fileDir), new File(filePath).getName());
				fos = new FileOutputStream(_f);
				bm.compress(Bitmap.CompressFormat.JPEG, 80, fos);
				filePath = _f.getAbsolutePath();
			}
		} catch (Exception e) {
			throw new Exception("系统异常");
		} finally {
			if (fos != null)
				fos.close();
		}
		return filePath;
	}

	/**
	 * 把bitmap转换成String
	 *
	 * @param filePath
	 * @return
	 */
	public static String bitmapToString(String filePath, int quality) {
		Bitmap bm = getSmallBitmap(filePath);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, quality, baos);
		byte[] b = baos.toByteArray();
		return Base64.encodeToString(b, Base64.DEFAULT);
	}
}
